/**
 *
 */
package cn.com.callback

import groovy.json.*

//导入grooby的json解析api包
import java.util.Map;
import cn.com.elite.EliteBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.Util;

/**
 * @author ThinkPad
 *
 */
public class SendMessage implements CallBackInvoke {
    private static final Log log = LogFactory.getLog(SendMessage.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String body) {
        def corpid = "301195"; //企业ID
        def loginname = "400"; //登录账号
        def password = "734095"; //登录密码
        def timestamp = map.get("timestamp"); //时间戳
        password = Util.getMD5Str(corpid + password + timestamp); //时间戳
        def content = map.get("content"); //获取短信内容
        def mobile = map.get("mobile"); //获取短信号码
        def url = "http://sms3.mobset.com:8080/Api?wsdl"; //请求URL
        def xmlbody = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<SOAP-ENV:Body>
		<m:Sms_Send xmlns:m="http://tempuri.org/">
			<m:CorpID>""" + corpid + """</m:CorpID>
			<m:LoginName>""" + loginname + """</m:LoginName>
			<m:Password>""" + password + """</m:Password>
			<m:TimeStamp>""" + timestamp + """</m:TimeStamp>
			<m:AddNum></m:AddNum>
			<m:Timer></m:Timer>
			<m:LongSms>1</m:LongSms>
			<m:MobileList>
				<m:MobileListGroup>
					<m:Mobile>""" + mobile + """</m:Mobile>
					<m:MsgID></m:MsgID>
				</m:MobileListGroup>
			</m:MobileList>
			<m:Content>""" + content + """</m:Content>
		</m:Sms_Send>
	</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
        log.debug("xmlbody:" + xmlbody);
        def charset = "utf-8";
        def timeout = 100000;
        def SOAPAction = "http://tempuri.org/Sms_Send";
        try {
            def httpresult = Util.callWebService(url, xmlbody, charset, SOAPAction, timeout);
            def xml = Util.Transform(httpresult); //去命名空间
            def list = new XmlParser().parseText(xml);
            def responsecode = list.Body.Sms_SendResponse.Count.text();
            def errormsg = list.Body.Sms_SendResponse.ErrMsg.text();
            log.debug("responsecode:" + responsecode + " |errormsg" + errormsg);
            def updatesql = ""; //申明变量
            def sqlResult = ""; //申明变量
            if (responsecode > 0) {
                updatesql = "update documentout set status=1,TRIALCOUNT=TRIALCOUNT+1,LASTTRYDATETIME=getdate(),MODIFIEDDATE=getdate(),MODIFIEDBY='SELITE' WHERE documentout_guid='" + map.get("DOCUMENTOUT_GUID") + "'";
            } else {
                updatesql = "update documentout set status=5,FAILDESCRIPTION='" + errormsg + "',TRIALCOUNT=TRIALCOUNT+1,LASTTRYDATETIME=getdate(),MODIFIEDDATE=getdate(),MODIFIEDBY='SELITE' WHERE documentout_guid='" + map.get("DOCUMENTOUT_GUID") + "'";
            };
            sqlResult = eliteBean.update(updatesql, 'dataSource');
        } catch (Exception e) {
            log.error("ErrorMessage", e);
            def updatesql2 = "update documentout set status=5,FAILDESCRIPTION='-9999',TRIALCOUNT=TRIALCOUNT+1,LASTTRYDATETIME=getdate(),MODIFIEDDATE=getdate(),MODIFIEDBY='SELITE' WHERE documentout_guid='" + map.get("DOCUMENTOUT_GUID") + "'";
            def sqlResult3 = eliteBean.update(updatesql2, 'dataSource');
        }
    }
}
